package com.processmining.util.algo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import org.deckfour.xes.model.XLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.processmining.entity.Algo;
import com.processmining.entity.EventLog;
import com.processmining.entity.MergeEventLog;
import com.processmining.service.IEventLogService;
import com.processmining.service.IMergeEventLogService;
import com.processmining.support.eventLogExport.EventLogExport;
import com.processmining.support.eventLogExport.impl.EventLogExportImpl;
import com.processmining.support.eventLogParse.EventLogParse;
import com.processmining.support.eventLogParse.impl.EventLogParseImpl;

@Service
public class MergeUtil {
	@Autowired
	private IEventLogService eventLogService;

	@Autowired
	private IMergeEventLogService mergeEventLogService;

	public EventLog merge(EventLog evtLog1, EventLog evtLog2, Algo algo, HashMap<String, Object> param, Integer uid)
			throws Exception {
		String path = algo.getPath();

		Integer evtLog1Id = evtLog1.getId();
		InputStream input = eventLogService.downloadLog(evtLog1Id);
		// InputStream input= new FileInputStream(new File("D:\\1"));
		EventLogParse parser = new EventLogParseImpl();
		XLog xlog1 = parser.eventLogParse(input);
		input.close();

		Integer evtLog2Id = evtLog2.getId();
		InputStream input2 = eventLogService.downloadLog(evtLog2Id);
		// InputStream input2=new FileInputStream(new File("D:\\2"));
		EventLogParse parser2 = new EventLogParseImpl();
		XLog xlog2 = parser2.eventLogParse(input2);
		input2.close();

		Class c = XLog.class;

		File file = new File(path);// jar包的路径
		// File file=new File("D:\\AIA.jar");
		URL url = file.toURI().toURL();
		ClassLoader loader = new URLClassLoader(new URL[] { url }, this.getClass().getClassLoader());// 创建类载入器
		Class<?> cls = loader.loadClass(algo.getPackagename() + "." + algo.getClassname());
		System.out.print(algo.getPackagename() + "." + algo.getClassname());// 载入指定类，注意一定要带上类的包名
		Method method = cls.getMethod(algo.getMethod(), c, c, HashMap.class);// 方法名和相应的各个參数的类型
		Object o = cls.newInstance();
		XLog result = (XLog) method.invoke(o, xlog1, xlog2, param);// 调用得到的上边的方法method(静态方法，第一个參数能够为null)

		EventLog resultEventLog = new EventLog();
		resultEventLog.setIsmerged(true);
		resultEventLog.setCreatetime(new Date());
		resultEventLog.setFormat("xes");
		resultEventLog.setName(UUID.randomUUID().toString().substring(0, 10) + ".xes");
		resultEventLog.setCreatorid(uid);
		resultEventLog.setIsshared(true);

		SummarizeTool st = SummarizeXLog.summarize(result);
		resultEventLog.setAverage(st.getAverage());
		resultEventLog.setProcessactivityevent(st.getProcessActivityEvent());
		resultEventLog.setTotaleventnum(st.getTotalEventNum());
		resultEventLog.setTotalinstancenum(st.getTotalInstanceNum());
		resultEventLog.setController(st.getController());

		File tempFile = null;
		try {
			tempFile = File.createTempFile("tmp", "log");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		EventLogExport ele = new EventLogExportImpl();
		ele.convertXLogToXesFile(result, tempFile);

		FileInputStream fis = new FileInputStream(tempFile);
		eventLogService.uploadLog(fis, resultEventLog);

		MergeEventLog mel = new MergeEventLog();
		if (eventLogService.selectByPK(evtLog1Id) != null) {
			mel.setSource1name(evtLog1.getName());
			mel.setSourceeventlog1id(evtLog1.getId());
		}
		if (eventLogService.selectByPK(evtLog2Id) != null) {
			mel.setSource2name(evtLog2.getName());
			mel.setSourceeventlog2id(evtLog2.getId());
		}
		mel.setTargeteventlogid(resultEventLog.getId());
		mel.setTargetname(resultEventLog.getName());

		mergeEventLogService.insert(mel);
		// eventLogService.uploadLog(inputStream, log)
		return resultEventLog;
	}

	public static void main(String[] args)
			throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException,
			ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException {
		// String path=algo.getPath();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("maxNumGen", 10000);
		map.put("numInitSol", 100);
		map.put("maxNumNoOpt", 50);
		map.put("percBestSolForClone", 0.1);
		map.put("scoreSameAttValue", 5);
		map.put("scoreTimeDiff", 5);
		/*
		 * Integer evtLog1Id = evtLog1.getId(); InputStream input =
		 * eventLogService.downloadLog(evtLog1Id);
		 */
		InputStream input = new FileInputStream(new File("D:\\1"));
		EventLogParse parser = new EventLogParseImpl();
		XLog xlog1 = parser.eventLogParse(input);
		input.close();

		/*
		 * Integer evtLog2Id = evtLog2.getId(); InputStream input2 =
		 * eventLogService.downloadLog(evtLog1Id);
		 */
		InputStream input2 = new FileInputStream(new File("D:\\2"));
		EventLogParse parser2 = new EventLogParseImpl();
		XLog xlog2 = parser2.eventLogParse(input2);
		input2.close();

		// File file = new File(path);//jar包的路径
		File file = new File("D:\\AIA.jar");
		URL url = file.toURI().toURL();
		ClassLoader loader = new URLClassLoader(new URL[] { url });// 创建类载入器
		Class<?> cls = loader.loadClass("net.ProcessMining.logMerge.aia.AIAMerger");// 载入指定类，注意一定要带上类的包名
		Method method = cls.getMethod("mergeAlgo", XLog.class, XLog.class, HashMap.class);// 方法名和相应的各个參数的类型
		Object o = cls.newInstance();
		System.out.print("xxx");
		XLog result = (XLog) method.invoke(o, xlog1, xlog2, map);// 调用得到的上边的方法method(静态方法，第一个參数能够为null)
		System.out.print("xxx");
		EventLog resultEventLog = new EventLog();
		resultEventLog.setIsmerged(true);
		// resultEventLog.setCreatetime(new Date());
		resultEventLog.setFormat("xes");
		resultEventLog.setName(UUID.randomUUID().toString().substring(0, 10));
		resultEventLog.setIsmerged(true);

		SummarizeTool st = SummarizeXLog.summarize(result);
		resultEventLog.setAverage(st.getAverage());
		resultEventLog.setProcessactivityevent(st.getProcessActivityEvent());
		resultEventLog.setTotaleventnum(st.getTotalEventNum());
		resultEventLog.setTotalinstancenum(st.getTotalInstanceNum());
		resultEventLog.setController(st.getController());
		System.out.print(st.getAverage());
		System.out.print(st.getProcessActivityEvent());
		System.out.print(st.getTotalEventNum());
		System.out.print(st.getTotalInstanceNum());
		System.out.print(st.getController());

		// File tempFile = null;
		// try {
		// tempFile = File.createTempFile("tmp", "log");
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		// EventLogExport ele=new EventLogExportImpl();
		// ele.convertXLogToXesFile(result, tempFile);
		//
		// FileInputStream fis=new FileInputStream(tempFile);
		// eventLogService.uploadLog(fis, resultEventLog);
		//
		// MergeEventLog mel=new MergeEventLog();
		// mel.setSource1name(evtLog1.getName());
		// mel.setSource2name(evtLog2.getName());
		// mel.setSourceeventlog1id(evtLog1.getId());
		// mel.setSourceeventlog2id(evtLog2.getId());
		// mel.setTargeteventlogid(resultEventLog.getId());
		// mel.setTargetname(resultEventLog.getName());
		//
		// mergeEventLogService.insert(mel);
		// //eventLogService.uploadLog(inputStream, log)
		// return null;
	}
}
